	
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;


public class DAOContact{
	private final static String RESOURCE_JDBC = "java:comp/env/jdbc/dsMyDB";
	public DAOContact(){}
	
		public String addContact( String nom, String prenom, String email,
	String adresse){
		try {
			final Context lContext = new InitialContext();
			final DataSource lDataSource = (DataSource) lContext
					.lookup(RESOURCE_JDBC);
			final Connection lConnection = lDataSource.getConnection();

			// adding a new contact
			final PreparedStatement lPreparedStatementCreation =

			lConnection
					.prepareStatement("INSERT INTO Contact(nom,prenom,email,adresse)VALUES (?,?,?,?)");								
							
			lPreparedStatementCreation.setString(0, nom);				
			lPreparedStatementCreation.setString(1, prenom);				
			lPreparedStatementCreation.setString(2, email);
			lPreparedStatementCreation.setString(3, adresse);
			lPreparedStatementCreation.executeUpdate();
			return null;
		} catch (NamingException e) {

			return "NamingException : " + e.getMessage();

		} catch (SQLException e) {

			return "SQLException : " + e.getMessage();
		}
	}
	
	// Modifying a contact (Primary key of table is first argument)
	public String modifyContact( String nom, String prenom, String email,
	String adresse){
	
	try {
			final Context lContext = new InitialContext();
			final DataSource lDataSource = (DataSource) lContext
					.lookup(RESOURCE_JDBC);
			final Connection lConnection = lDataSource.getConnection();

			// modify a new contact
			final PreparedStatement lPreparedStatementCreation =
			
			lConnection
					.prepareStatement("UPDATE Contact SET prenom=:1,email=:2, adresse=:3 WHERE nom = :0)");
					
					
					
							
			lPreparedStatementCreation.setString(0, nom);				
			lPreparedStatementCreation.setString(1, prenom);				
			lPreparedStatementCreation.setString(2, email);
			lPreparedStatementCreation.setString(3, adresse);
			lPreparedStatementCreation.executeUpdate();		
				return null;
		} catch (NamingException e) {

			return "NamingException : " + e.getMessage();
			

		} catch (SQLException e) {

			return "SQLException : " + e.getMessage();
		}
	}
	
	
	public String deleteContact( String	nom){
		try {
			final Context lContext = new InitialContext();
			final DataSource lDataSource = (DataSource) lContext
					.lookup(RESOURCE_JDBC);
			final Connection lConnection = lDataSource.getConnection();

			// modify a new contact
			final PreparedStatement lPreparedStatementCreation =
	
			lConnection
					.prepareStatement("DELETE FROM Contact WHERE nom=:0");
					
							
			lPreparedStatementCreation.setString(0, nom);
			lPreparedStatementCreation.executeUpdate();
			
			return null;
		} catch (NamingException e) {

			return "NamingException : " + e.getMessage();
			

		} catch (SQLException e) {

			return "SQLException : " + e.getMessage();
		}
	
	}
	
	public String listContact( String nom, String prenom, String email,
	String adresse){
	return null;
	}
	
	
	
} 